package xps.and.uudaijia.userclient.presenter;

import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import xps.and.uudaijia.userclient.view.activity.ObtainAddressActivty;

public class ObtainAddressPresenter extends BasePresenter<ObtainAddressActivty> implements OnGetPoiSearchResultListener {
    Disposable disposable = null;

    @Override
    protected void onTakeView(ObtainAddressActivty ac) {
        super.onTakeView(ac);
        ac.mPoiSearch.setOnGetPoiSearchResultListener(this);
        startSearch(ac);
    }

    public void startSearch(ObtainAddressActivty ac ){
        disposable =  RxTextView.textChangeEvents(ac.et_des).debounce(300, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((e) ->
                        serachInCity(ac, e.text())
                );
    }

    private void serachInCity(ObtainAddressActivty obtainAddressActivty, CharSequence keyWord) {
        obtainAddressActivty.mPoiSearch.searchInCity((new PoiCitySearchOption())
                .city(obtainAddressActivty.tv_city.getText().toString())
                .keyword(keyWord + "")
                .pageCapacity(10)
                .pageNum(0));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
    }

    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        if (poiResult == null || poiResult.getAllPoi() == null || poiResult.getAllPoi().size() <= 0)
            return;

        deliver((ac) -> ac.setSearchData(poiResult.getAllPoi())
                , (ac) ->{});
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }
}
